export const formatData = (arr) => {
  return arr.map(({ _id, title, description, avatar }) => ({
    id: _id,
    title,
    description,
    avatar: avatar.url + "?x-oss-process=image/resize,m_pad,h_100,w_100",
  }));
};

export const arrToObj = <T extends { _id?: string }>(arr: Array<T>) => {
  return arr.reduce((prev, current) => {
    if (current._id) {
      prev[current._id] = current;
    }
    return prev;
  }, {} as { [key: string]: T });
};

export const objToArr = <T>(obj: { [key: string]: T }) => {
  return Object.keys(obj).map((key) => obj[key]);
};

interface CheckCondition {
  format?: string[];
  size?: number;
}
type ErrorType = "size" | "format" | null;

export const beforeUploadCheck = (file: File, condition: CheckCondition) => {
  const { format, size } = condition;
  const isValidFormat = format ? format.includes(file.type) : true;
  const isValidSize = size ? file.size / 1024 / 1024 < size : true;
  let error: ErrorType = null;
  if (!isValidFormat) {
    error = "format";
  }
  if (!isValidSize) {
    error = "size";
  }
  return {
    passed: isValidFormat && isValidSize,
    error,
  };
};
